<template>
  <el-card>
    <el-form
      :model="formBase"
      label-width="80px"
      label-position="right"
      size="small"
    >
      <el-row>
        <el-col :span="6">
          <el-form-item label="标签名称">
            <el-input v-model="formBase.tags" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <!-- 备注 -->
          <el-form-item label="城市" prop="province">
            <el-select
              class="filter-item"
              style="display: block; width: 100%"
              v-model="formBase.province"
              @keyup.enter="handleFilter"
              @change="handleProvince"
              filterable
            >
              <el-option
                v-for="item in citySelect.province"
                :key="item"
                :label="item"
                :value="item"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="地区" prop="city">
            <el-select
              class="filter-item"
              style="display: block; width: 100%"
              v-model="formBase.city"
              @keyup.enter="handleFilter"
              filterable
            >
              <el-option
                v-for="item in citySelect.cityDate"
                :key="item"
                :label="item"
                :value="item"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item label="企业简称">
            <el-input
              v-model="formBase.shortName"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="状态">
            <el-select
              v-model="formBase.state"
              style="display: block; width: 100%"
              placeholder="请选择"
            >
              <el-option label="启用" :value="1"></el-option>
              <el-option label="禁用" :value="0"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-button size="small" style="margin-left: 40px" @click="clear"
            >清除</el-button
          >
          <el-button type="primary" size="small" @click="getList"
            >搜索</el-button
          >
        </el-col>
        <el-col :span="6">
          <el-form-item> </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item>
            <el-button
              type="success"
              icon="el-icon-edit"
              style="float: right"
              @click="AddUser(0)"
              >新增用户</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-alert
      :title="`数据一共${data.counts}行`"
      type="info"
      style="margin-bottom: 20px"
      :closable="false"
      show-icon
    >
    </el-alert>
    <el-table
      :data="data.items"
      :header-cell-style="{ backgroundColor: '#fafafa', textAlign: 'center' }"
      :cell-style="{ textAlign: 'center' }"
    >
      <el-table-column label="序号" prop="id"> </el-table-column>
      <el-table-column label="企业编号" prop="number"> </el-table-column>
      <el-table-column label="企业简称" prop="shortName"> </el-table-column>
      <el-table-column label="标签" prop="tags"> </el-table-column>
      <el-table-column label="创建者" prop="creatorID"> </el-table-column>
      <el-table-column label="创建日期" prop="addDate"> </el-table-column>
      <el-table-column label="备注" prop="remarks"> </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="{ row }">
          {{ row.state === 0 ? '禁用' : '启用' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="{ row }">
          <el-button
            type="primary"
            plain
            icon="el-icon-edit"
            circle
            @click="AddUser(row)"
          ></el-button>
          <el-button
            v-if="row.state == 0"
            type="success"
            plain
            icon="el-icon-check"
            circle
            @click="onSwitch(row)"
          ></el-button>
          <el-button
            v-else
            type="warning"
            plain
            icon="el-icon-close"
            circle
            @click="onSwitch(row)"
          ></el-button>
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            circle
            @click="delUser(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="formBase.page"
      :page-sizes="[3, 5, 10]"
      :page-size="formBase.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="data.counts"
    >
    </el-pagination>
    <el-dialog :title="`${titleID}用户`" :visible.sync="dialogFormVisible">
      <el-form
        :rules="ruleInline"
        ref="dataForm"
        :model="formBase"
        label-position="left"
        label-width="150px"
        style="width: 80%; margin-left: 10px"
      >
        <el-form-item label="企业名称" prop="shortName">
          <el-input v-model="formBase.shortName"></el-input>
        </el-form-item>
        <el-form-item label="所属公司" prop="company">
          <el-input v-model="formBase.company"></el-input>
          <p>https://www.tianyancha.com （在此可查询所属公司全称及简称）</p>
        </el-form-item>
        <el-form-item label="城市" prop="province">
          <el-select
            class="filter-item"
            style="width: 202px; margin-right: 20px"
            v-model="formBase.province"
            @keyup.enter="handleFilter"
            @change="handleProvince"
            filterable
          >
            <el-option
              v-for="item in citySelect.province"
              :key="item"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
          <el-select
            class="filter-item"
            style="width: 203px"
            v-model="formBase.city"
            @keyup.enter="handleFilter"
            filterable
          >
            <el-option
              v-for="item in citySelect.cityDate"
              :key="item"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="方向（企业标签）" prop="tags">
          <el-input v-model="formBase.tags"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4 }"
            placeholder="请输入"
            v-model="formBase.remarks"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="createData">确定</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import { provinces, citys } from '@/api/hmmm/citys.js'
import { list, disabled, add, remove, update } from '@/api/hmmm/companys.js'
export default {
  components: {},
  data () {
    return {
      citySelect: {
        province: [],
        cityDate: []
      },
      // 表单验证
      ruleInline: {
        shortName: [
          { required: true, message: '企业简称不能为空', trigger: 'blur' }
        ],
        province: [
          { required: true, message: '请选择省份', trigger: 'change' }
        ],
        tags: [{ required: true, message: '请输入标签', trigger: 'blur' }]
      },
      dialogFormVisible: false,
      data: {},
      provinces,
      citys,
      formBase: {
        state: null,
        city: '',
        page: 1,
        pagesize: 10,
        isFamous: true,
        shortName: '',
        company: '',
        tags: '',
        remarks: ''
      },
      titleID: '新增'
    }
  },
  created () {
    this.getList()
    this.getCityData()
  },
  methods: {
    close () {
      this.formBase = {
        isFamous: true,
        shortName: '',
        company: '',
        tags: '',
        remarks: ''
      }
      // console.log(321)
      this.$refs.dataForm.resetFields()
      this.dialogFormVisible = false
    },
    // 清除列表
    clear () {
      this.formBase = {
        state: null,
        city: '',
        page: 1,
        pagesize: 10,
        isFamous: true,
        shortName: '',
        company: '',
        tags: '',
        remarks: ''
      }
    },
    // 获取列表
    async getList () {
      const { data } = await list(this.formBase)
      // console.log(items)
      this.data = data
    },
    // pageSize每页显示条目个数 改变时会触发
    handleSizeChange (val) {
      this.formBase.pagesize = val
      this.getList()
    },
    // currentPage当前页数 改变时会触发
    handleCurrentChange (val) {
      this.formBase.page = val
      this.getList()
    },
    // 启用
    async onSwitch (row) {
      await this.$confirm('已成功禁用, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await disabled({ id: row.id, state: row.state === 0 ? 1 : 0 })
          this.$message({
            type: 'success',
            message: `已成功${row.state === 0 ? '禁用' : '启用'}`
          })
          await this.getList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作！'
          })
        })
    },
    // 添加编辑用户
    AddUser (row) {
      if (row) {
        this.titleID = '编辑'
        const isFamous = row.isFamous === 1
        this.formBase = { ...row, isFamous }
      } else {
        this.titleID = '新增'
      }
      this.dialogFormVisible = true
    },
    // 删除用户
    async delUser (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$message({
            type: 'success',
            message: '删除成功'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作！'
          })
        })
      await remove(row.id)
      this.getList()
    },
    // 复制样式
    // 获取省
    getCityData: function () {
      this.citySelect.province = provinces()
    },
    // 选省获取到市
    handleProvince: function (e) {
      this.citySelect.cityDate = citys(e)
      this.formBase.city = this.citySelect.cityDate[0]
    },
    // 表单提交
    async createData () {
      await this.$refs.dataForm.validate()
      if (this.formBase.id) {
        await update(this.formBase)
      } else {
        await add(this.formBase)
      }
      this.$message.success('新增成功')
      this.getList()
      // console.log(123)

      this.formBase = {
        isFamous: true,
        shortName: '',
        company: '',
        tags: '',
        remarks: ''
      }
      // console.log(321)
      this.$refs.dataForm.resetFields()
      this.dialogFormVisible = false
    }
  }
}
</script>

<style scoped>
.el-card {
  margin: 20px;
}
.el-pagination {
  text-align: right;
  margin-top: 20px;
  white-space: nowrap;
  padding: 2px 5px;
  color: #303133;
  font-weight: 700;
}
</style>
